package com.taptech.doufu.util.ui

import android.app.AppOpsManager
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.provider.Settings

object  NotificationUtils {

    /**
     * 是否开启通知栏权限
     */
    fun areNotificationsEnabled(context: Context, channelId: String): Boolean {
        when {
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.N -> {
                val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
                return if (manager.areNotificationsEnabled()) {
                    //通知权限开启
                    if (channelId.isNotEmpty() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                        //此处判断通知渠道是否开启
                        val channel = manager.getNotificationChannel(channelId)
                        channel.importance != NotificationManager.IMPORTANCE_NONE
                    } else {
                        true
                    }
                } else {
                    //通知权限未开启
                    false
                }
            }
            Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT -> {
                try {
                    val mAppOps = context.getSystemService(Context.APP_OPS_SERVICE) as AppOpsManager
                    val appInfo = context.applicationInfo
                    val pkg = context.applicationContext.packageName
                    val uid = appInfo.uid
                    val appOpsClass = Class.forName(AppOpsManager::class.java.name)
                    val checkOpNoThrowMethod = appOpsClass.getMethod(
                        "checkOpNoThrow", Integer.TYPE, Integer.TYPE,
                        String::class.java
                    )
                    val opPostNotificationValue = appOpsClass.getDeclaredField("OP_POST_NOTIFICATION")

                    val value = opPostNotificationValue.get(Int::class.java) as Int
                    return checkOpNoThrowMethod.invoke(
                        mAppOps,
                        value,
                        uid,
                        pkg
                    ) as Int == AppOpsManager.MODE_ALLOWED

                } catch (e: Exception) {
                    e.printStackTrace()
                }
                return false
            }
            else -> return true  //4.4以下版本
        }
    }

    /**
     * 创建通知渠道
     */
    fun createNotificationChannel(context: Context, channelId: String, channelName: String,importance:Int) {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
            val notificationChannel =
                NotificationChannel(channelId, channelName, importance)
            notificationChannel.enableLights(true)
            val nm = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            nm.createNotificationChannel(notificationChannel)
        }
    }

    /**
     * 跳转到通知权限设置页面
     * @param context
     */
    fun gotoNotificationSetting(context: Context) {
        val localIntent = Intent()
        // < 直接跳转到应用通知设置的代码
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            localIntent.action = Settings.ACTION_APP_NOTIFICATION_SETTINGS
            localIntent.putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            localIntent.action = "android.settings.APP_NOTIFICATION_SETTINGS"
            localIntent.putExtra("app_package", context.packageName)
            localIntent.putExtra("app_uid", context.applicationInfo.uid)
        } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
            localIntent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
            localIntent.addCategory(Intent.CATEGORY_DEFAULT)
            localIntent.data = Uri.parse("package:" + context.packageName)
        } else {
            // < 4.4以下没有从app跳转到应用通知设置页面的Action，可考虑跳转到应用详情页面,
            localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            if (Build.VERSION.SDK_INT >= 9) {
                localIntent.action = "android.settings.APPLICATION_DETAILS_SETTINGS"
                localIntent.data = Uri.fromParts("package", context.packageName, null)
            } else if (Build.VERSION.SDK_INT <= 8) {
                localIntent.action = Intent.ACTION_VIEW
                localIntent.setClassName("com.android.settings", "com.android.setting.InstalledAppDetails")
                localIntent.putExtra("com.android.settings.ApplicationPkgName", context.packageName)
            }
        }
        context.startActivity(localIntent)
    }
}